package com.cjm.one.service.impl;

import com.cjm.one.payment.client.PaymentInfoClient;
import com.cjm.one.service.AliPayService;
import feign.FeignException;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

@Service
@Slf4j
public class AliPayServiceImpl implements AliPayService {
    @Resource
    private PaymentInfoClient paymentInfoClient;
    @Override
    public void alipay(String orderNo,HttpServletResponse response) throws IOException {

        try {
            String html = paymentInfoClient.pay(orderNo).getData();

            response.setContentType("text/html;charset=UTF-8");
            response.setCharacterEncoding("UTF-8");

            try (PrintWriter writer = response.getWriter()) {
                writer.write(html);
                writer.flush();
                writer.close();
            }
        } catch (FeignException e) {
            response.sendError(HttpStatus.INTERNAL_SERVER_ERROR.value(), "支付服务调用失败");
            log.error("Feign调用异常", e);
        }
    }

    @Override
    public String payNotify(Map<String, String> params) throws Exception {
        return paymentInfoClient.payNotify(params).getData();
    }
}
